package choreganizer.calendar;

import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JPanel;
import javax.swing.JLabel;

/**
 * @version 1.0
 * @created 04-May-2007 7:45:44 PM
 */
public class CalendarTableModel extends javax.swing.table.AbstractTableModel {

    public enum Days {

	Sunday, Monday, Tuesday, Wednesday, Thrusday, Friday, Saturday
    }

    
    
      ;
    public  CalendarTableItem _CalandarTableItem;

    private  Object 
	   [][]          _ModelData;
    
    
    public CalendarTableModel()
    {
        // Create a single, default panel to display in the table when the table
        // is created
        _ModelData = new Object[1][1];
	_ModelData[0][0] = new String("No Account Opened");
    }

    /*
     * Month = 1 or 2 digit month (0 = january)
     * Year = 4 digit year
     */
    synchronized public void display(int userMonth, int userYear) {
	// Figure out some info about the mont/year to display
	GregorianCalendar displayCalendar = new GregorianCalendar(userYear, userMonth, 1);
	int firstOfMonth = displayCalendar.get(Calendar.DAY_OF_WEEK) - 1;
	int numDays = displayCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	int numRows = (firstOfMonth + numDays <= 35 ? 5 : 6);

	// Figure out if the month/year to display is the current month/year
	GregorianCalendar currentCalendar = new GregorianCalendar();
	boolean isCurrYearMonth = false;
	int todaysDayOfMonth = 0;
	if (currentCalendar.get(Calendar.MONTH) == displayCalendar.get(Calendar.MONTH) &&
		currentCalendar.get(Calendar.YEAR) == displayCalendar.get(Calendar.YEAR)) {
	    isCurrYearMonth = true;
	    todaysDayOfMonth = currentCalendar.get(Calendar.DAY_OF_MONTH);
	}

	// Initialize table data array
	_ModelData = new Object[numRows][7];

	// Populate the table cells
	int currDate = 1;
	for (int i = 0; i < numRows * 7; i++) {
	    int week = (i / 7);	// the quotient = the week
	    int day = (i % 7);	// the remainder = the day

	    // This cell is an actual day of the display month
	    if (i >= firstOfMonth && i < numDays + firstOfMonth) {
		//_ModelData[week][day] = new JPanel().add(new JLabel("Day: "+i));
		_ModelData[week][day] = new String("Day: " + currDate);
		currDate++;
	    } // This cell isn't for a day of the display month
	    else {
		_ModelData[week][day] = new String("");
	    //_ModelData[week][day] = new JPanel();
	    }
	}

	// Cleanup
	displayCalendar = null;
	currentCalendar = null;
    }

    public void clear() {
	_ModelData = new Object[0][0];
    }

    @Override
    public Object getValueAt(int row, int col) {
	return _ModelData[row][col];
    }

    @Override
    public int getColumnCount() {
	if (_ModelData.length > 0) {
	    return _ModelData[0].length;
	} //return Days.values().length;
	else {
	    return 0;
	}
    }

    @Override
    public int getRowCount() {
	return _ModelData.length;
    }

    @Override
    public String getColumnName(int col) {
	if (_ModelData.length > 0) {
	    Object[] d = Days.values();
	    return d[col].toString();
	} else {
	    return "";
	}
    }

    @Override
    public Class getColumnClass(int cell) {
	// We'll set our return class to a basic JPanel
	Class c = JPanel.class;

	// Attempt to find the class of the renderer used for cell
	// while catching any errors (like cell == null)
	try {
	    c = getValueAt(0, cell).getClass();
	} // Whatever happens, return our class c
	finally {
	    return c;
	}
    }

    @Override
    public boolean isCellEditable(int row, int col) {
	return false;
    }
}
